﻿var Qv = new Array(); 
var Qk = new Array(); 
var myDiv; 
var flag;
var x;

showSubmit = false;

var campi = {
    1: "aggettivo",
    2: "avverbio",
    3: "congiunzione",
    4: "determinativo",
    5: "preposizione",
    6: "interiezione",
    7: "numero",
    8: "pronome",
    9: "punteggiatura",
    10: "articolo",
    11: "nome",
    12: "abbreviazione",
    13: "verbo",
    14: "classe residua"
};

associazioni = new Array(14);

function stampa(){
			
		Qv = iDocument.split('|||');	
		Qk = knowledge.split('|||');	
		myDiv = Qv.length;	
				
		normal_state();
		
        var stringa = "";
        
        for (var i = 0; i < Qv.length; i++) {
			stringa += "<div class=\"draggable\" id=\""+i+"\" >" + Qv[i] + "</div>";
        }

        $("#space").html(stringa);

        $('.draggable').draggable({
            opacity: 0.8,
            revert: 'invalid',
            containment: '#gameArea',
            fx: 300,
            start: function(event, ui) {
                //associazioni[$(ui.droppable).attr("id")]="";
            },
            stop: function(event, ui) {
                //alert($(this).css('top') + " e " + $(this).css('left'))
            }
        });

        $(".categories > li").droppable({
            //activeClass: 'ui-state-hover',
            hoverClass: 'ui-state-active',
            tollerance: 'fit',
            drop: function(event, ui) {
                $(this).addClass('ui-state-highlight');
                associazioni[$(this).attr("id") - 1] = $(ui.draggable).text();
                var p = $(ui.draggable).detach();
                p.appendTo(this);
				
				idp = parseInt(p.attr("id"));
                idb = $(this).attr("id");

				if(idb == (Qk[idp]))
                  flag = 0;
                 else{
                  flag = 1;
                  Qk[idp] = idb;
                 }
				 
				 function user_feedback(flag)
				{
					if(x)
					{
						clearTimeout(x);
						normal_state();
					}
					if(flag ==0)
    				{
						$("#validation").show();
						$("#wait").hide();
						$("#err_validation").hide();	
				    }
    				else
    				{
						$("#err_validation").show();
						$("#wait").hide();
						$("#validation").hide();
    				}
					x = setTimeout ( "normal_state()", 1000 );
}

user_feedback(flag);
                 
                 //alert(flag);
				
                p.css("position", "absolute");
                var diff = ($(this).outerHeight()-p.outerHeight())/2;
                p.css("top", diff);
                p.css("left", ($(this).width() - p.outerWidth()) / 2);
                var shift = (($(this).outerHeight()) / 2);
                var shift2 = (($(this).width()) / 2);
                p.animate({ "padding-top": "0px", "padding-bottom": "0px",  "line-height": "0px", "font-size": "0px", "top": shift }, 1000, function() {
                p.remove();
				
				myDiv-- ;
				if(myDiv == 0){
					send(scoreDiv,"Il tuo<br/>punteggio: <br/>","<br/>Bravo!!!");
				}
               });
            },
            out: function(event, ui) {
                if ($(ui.draggable).text() == associazioni[$(this).attr("id") - 1]) {
                    $(this).removeClass('ui-state-highlight');
                    associazioni[$(this).attr("id") - 1] = "undefined";
                }
            }
        });

        $('#dialog').dialog({
            autoOpen: false,
            width: 300,
            buttons: {
                "Ok": function() {
                    $(this).dialog("close");
                },
                "Annulla": function() {
                    $(this).dialog("close");
                }
            }
        });
        $('#dialog_link').click(function() {
            $('#dialog').dialog('open');
            return false;
        });
        $('#dialog_link, ul#icons div').hover(
					function() { $(this).addClass('ui-state-hover'); },
					function() { $(this).removeClass('ui-state-hover'); }
		);
        $('#clicca').click(function() {
            $('#qui').html(function() {
                var testo = "{";
                for (i = 0; i < associazioni.length; i++) {
                    if (typeof (associazioni[i]) != "undefined") {
                        testo += "\"" + associazioni[i] + "\" : \"" + campi[i + 1] + "\"";
                        if (true) testo += ", ";
                    }
                }
                testo += "}";
                var json = eval("(" + testo + ")");
                $('#qui').html("<div class=\"sfondo\">" + testo + "</div>");
                //$.post("Default.aspx", testo);
            });
        });
        return false;
	}

function prepareAnswer(){
	answer = "";
	for(i = 0; Qk.lenght; i++){
		answer += Qv[i]+":"+Qk[i];
	}
}

function score(prevText, afterText) {
    hT= new Array();
    this.fontSize = "";
    this.bold = false;
    this.italic = false;
    this.color = "black";
    //this.fontFamily = "";
    this.background = "";
    this.prevText = "";
    this.afterText = "";
    this.render = function() {
        if (this.bold) hT['font-weight'] = "bold";
        if (this.italic) hT['font-style'] = "italic";
        hT["color"] = this.color;
        hT["font-size"] = this.fontSize;
        for (var i in hT) {
            stringa += i + ":" + hT[i] + "; ";
        }
        stringa = "<div style=\"" + stringa + "\"></div>";
        return stringa;
    }
}